package com.wdd.atcrowdfunding.manager.service.impl;

import com.wdd.atcrowdfunding.bean.Permission;
import com.wdd.atcrowdfunding.bean.Role;
import com.wdd.atcrowdfunding.bean.User;
import com.wdd.atcrowdfunding.exception.LoginFailException;
import com.wdd.atcrowdfunding.exception.UserException;
import com.wdd.atcrowdfunding.manager.dao.RolePermissionMapper;
import com.wdd.atcrowdfunding.manager.dao.UserMapper;
import com.wdd.atcrowdfunding.manager.dao.UserRoleMapper;
import com.wdd.atcrowdfunding.manager.service.UserService;
import com.wdd.atcrowdfunding.util.Const;
import com.wdd.atcrowdfunding.util.MD5Util;
import com.wdd.atcrowdfunding.util.Page;
import com.wdd.atcrowdfunding.vo.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public User queryUserLogin(Map<String, Object> paramMap) {
        User user = userMapper.queryUserLogin(paramMap);
        return user;
    }

    @Override
    public Page<User> queryUserPage(Map<String,Object> paramMap) {
        Page<User> page = new Page<User>((Integer) paramMap.get("pageno"),(Integer) paramMap.get("pagesize"));

        Integer startIndex = page.getStartIndex();
        paramMap.put("startIndex",startIndex);
        List<User> datas = userMapper.queryList(paramMap);
        page.setDatas(datas);

        Integer totalsize = userMapper.queryCount(paramMap);
        page.setTotalsize(totalsize);
        return page;
    }

    @Override
    public int saveUser(User user) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createtime = sdf.format(new Date());
        user.setCreatetime(createtime);
        user.setUserpswd(MD5Util.digest(Const.PASSWORD));
        return userMapper.insert(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public int deleteUser(Integer id) {
        userRoleMapper.deleteByUserid(id);
        return userMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public int deleteBatchUserByVO(Data data) throws UserException {
//        int totalCount = 0;
//        try{
//            for(Integer id : ids){
//                int count = userMapper.deleteByPrimaryKey(id);
//                totalCount += count;
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new UserException("批量删除管理员失败");
//        }
        userRoleMapper.deleteByVoUserid(data);

        return userMapper.deleteBatchUserByVO(data);
    }

    @Override
    public List<Role> queryAllRole() {
        return userMapper.queryAllRole();
    }

    @Override
    public List<Integer> queryRoleByUserid(Integer id) {
        return userMapper.queryRoleByUserid(id);
    }

    @Override
    public List<Permission> queryPermissionByUserid(Integer id) {
        return userMapper.queryPermissionByUserid(id);
    }

    @Override
    public boolean querySelectUserByLoginacct(String loginacct) {
        if(userMapper.querySelectUserByLoginacct(loginacct)==null){
            return false;
        }
        return true;
    }

    @Override
    public boolean querySelectUserByUsername(String username) {
        if(userMapper.querySelectUserByUsername(username)==null){
            return false;
        }
        return true;
    }

    @Override
    public boolean querySelectUserByEmail(String email) {
        if(userMapper.querySelectUserByEmail(email)==null){
            return false;
        }
        return true;
    }


}
